﻿using System;

namespace HomeWork1
{
    class Program
    {

        public class Node<T>
        {
            public Node<T> Next { get; set; }
            public T Data { get; set; }
            public Node(T t)
            {
                Next = null;
                Data = t;
            }
        }

        public class GenericList<T>
        {
            private Node<T> head;
            private Node<T> tail;

            public GenericList()
            {
                tail = head = null;
            }

            public Node<T> Head
            {
                get => head;
            }

            public void Add(T t)
            {
                Node<T> n = new Node<T>(t);
                if (tail == null)
                    head = tail = n;
                else
                {
                    tail.Next = n;
                    tail = n;
                }
            }
            public void ForEach(Action action) { action(); }
        }

        static void Main(string[] args)
        {
            int n = 0;
            GenericList<int> intList = new GenericList<int>();
            for(int i = 1; i <= 10; i++)
            {
                intList.Add(i);
            }
            Node<int> node = intList.Head;
            Console.Write("输出链表的全部元素如下：");
            intList.ForEach(() => {
                while(node != null){
                    Console.Write(node.Data.ToString() + "  ");
                    node = node.Next;
                } 
            });
            node = intList.Head;
            intList.ForEach(() => {
                while(node != null){
                    if(n <= node.Data)
                        n = node.Data;
                    node = node.Next;
                } 
            });
            Console.WriteLine(" ");
            Console.WriteLine("链表最大元素如下：" + n);
            node = intList.Head;
            intList.ForEach(() => {
                while(node != null){
                    if(n >= node.Data)
                        n = node.Data;
                    node = node.Next;
                } 
            });
            Console.WriteLine("链表最小元素如下：" + n);
            node = intList.Head;
            double num = 0;
            intList.ForEach(() => {
                while(node != null){
                    num += node.Data;
                    node = node.Next;
                } 
            });
            Console.WriteLine("链表元素平均值如下：" + num / 10);
        }
    }
}
